package com.ruoyi.event.controller;

import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.event.domain.EventImageList;
import com.ruoyi.event.service.IEventImageListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动图片信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/event/list")
public class EventImageListController extends BaseController
{
    @Autowired
    private IEventImageListService eventImageListService;

    @Autowired
    private FileUploadUtils fileUploadUtils;

    /**
     * 查询活动图片信息列表
     */
    @Log(title = "查询swiper图片信息", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(EventImageList eventImageList)
    {
        startPage();
        List<EventImageList> list = eventImageListService.selectEventImageListList(eventImageList);
        return getDataTable(list);
    }

    /**
     * 导出活动图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('event:list:export')")
    @Log(title = "活动图片信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EventImageList eventImageList)
    {
        List<EventImageList> list = eventImageListService.selectEventImageListList(eventImageList);
        ExcelUtil<EventImageList> util = new ExcelUtil<EventImageList>(EventImageList.class);
        util.exportExcel(response, list, "活动图片信息数据");
    }

    /**
     * 获取活动图片信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('event:list:query')")
    @GetMapping(value = "/{imageid}")
    public AjaxResult getInfo(@PathVariable("imageid") Long imageid)
    {
        return success(eventImageListService.selectEventImageListByImageid(imageid));
    }

    /**
     * 新增活动图片信息
     */
    @Log(title = "活动图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventImageList eventImageList)
    {
        return toAjax(eventImageListService.insertEventImageList(eventImageList));
    }

    /**
     * 修改活动图片信息
     */
    @PreAuthorize("@ss.hasPermi('event:list:edit')")
    @Log(title = "活动图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventImageList eventImageList)
    {
        return toAjax(eventImageListService.updateEventImageList(eventImageList));
    }

    /**
     * 删除活动图片信息
     */
    @PreAuthorize("@ss.hasPermi('event:list:remove')")
    @Log(title = "活动图片信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{imageids}")
    public AjaxResult remove(@PathVariable Long[] imageids)
    {
        return toAjax(eventImageListService.deleteEventImageListByImageids(imageids));
    }

    /**
     * 通用图片上传接口（阿里云OSS版）
     */
    @PostMapping("/uploadImage")
    public AjaxResult uploadFileImage(MultipartFile file, HttpServletRequest request) {
        try {
            // 调用工具类上传到阿里云OSS
            String url = fileUploadUtils.uploadImage(file);
            // 返回上传结果（若依前端要求的格式）
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);          // 图片访问URL
            ajax.put("fileName", url.substring(url.lastIndexOf("/") + 1));  // 文件名
            ajax.put("originalFilename", file.getOriginalFilename());       // 原始文件名
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }


}
